package com.myschool.server.web.controller;

import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.myschool.server.manager.GradeManager;
import com.myschool.server.manager.SectionManager;
import com.myschool.server.manager.UserManager;
import com.myschool.server.model.Address;
import com.myschool.server.model.Contacts;
import com.myschool.server.model.Grade;
import com.myschool.server.model.Json;
import com.myschool.server.model.RegistrationStatus;
import com.myschool.server.model.Rows;
import com.myschool.server.model.Section;
import com.myschool.server.model.User;
import com.myschool.server.model.UserType;

@Controller
public class StudentsController {
	@Autowired
	UserManager userManager;

	@Autowired
	private GradeManager gradeManager;
	

	@Autowired
	private SectionManager sectionManager;
	
	@RequestMapping("getStudents.htm")
	public @ResponseBody
	Json createTeacher(HttpServletRequest request, String sidx, String sord,
			int page, int rows, Boolean _search, String searchField,
			String searchOper, String searchString) {
		String section = request.getParameter("section");
		Json jsonData = new Json();
		
		if(section == "")
			return jsonData;
		
		try

		{

			List<User> users = userManager.retrieveStudentBySection(Long.valueOf(section));

			int userCount = users.size();

			jsonData.page = page;
			jsonData.total = userCount;
			jsonData.record = userCount;

			jsonData.rows = new Rows[userCount];
			for (int count = 0; count < userCount; count++) {
				User user = users.get(count);
				Rows row = new Rows();
				row.i = count + 1;
				row.cell = new String[] { user.getEntityId().toString(),
						user.getFirstName(), user.getLastName(), 
						"" };//user.getAddress().getContact().getEmail()
				jsonData.rows[count] = row;
			}
			

		} catch (Exception ex) {
			System.out.println(ex.getMessage());

		}

		return jsonData;
	}

	@RequestMapping("studentView.htm")
	public ModelAndView studentView() {

		Set<Grade> gradeList = gradeManager.retrieveAllGrades(1L);
		 ModelAndView view = new ModelAndView("StudentsConfig");
		 view.addObject("gradeList",gradeList);
		 return view;
	}

	@RequestMapping("addEditStudent.htm")
	public void addUser(HttpServletRequest request, HttpServletResponse response) {
		try {

			String fistName = request.getParameter("firstName");
			String sectionsStr = request.getParameter("section");
			String emailAddress = request.getParameter("emailAddress");
			String operation = request.getParameter("oper");

			Section section = sectionManager.retrieve(Long.valueOf(sectionsStr));
			
			
			// Get Students record
			User student = new User();
			student.setFirstName(fistName);
			student.setUsername(fistName);
			student.setLastName(request.getParameter("lastName"));
			Address address = new Address();
			Contacts contact = new Contacts();
			contact.setEmail(emailAddress);

			student.setAddress(address);

			  
			Address parentAddress = new Address();

			Contacts parentContact = new Contacts();
			parentContact.setEmail(request.getParameter("parentEmail"));
			parentAddress.setContact(parentContact); 
 
			// user.setEmailAddress(request.getParameter("EmailAddress"));
			student.setRegistrationStatus(RegistrationStatus.PR);// to_do remove
																	// this
			student.setType(UserType.STUDENT);
			student.setSection(section);
			if (operation.trim().equals("add")) {

				student = this.userManager.create(student); 
			}
			else { 
				String idStr = request.getParameter("userid"); 
				Long userID = Long.parseLong(idStr);
				student.setEntityId(userID);
				this.userManager.update(student);

			}
			// System.out.println(user.getFirstName());
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}

	}

	@RequestMapping("deleteStudent.htm")
	public void deleteUser(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			String userIDParameter = request.getParameter("userID");
			String[] userIDArray = userIDParameter.split(";");
			User user;
			for (String userID : userIDArray) {
				if (userID == "")
					continue;

				user = new User();
				Long id = Long.parseLong(userID.trim());
				user.setEntityId(id);
				this.userManager.delete(user);
			}

		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}
}
